import Vue from "vue";
import Vuex from "vuex";
import router from "vue-router";
import tagAdmin from "./modules/tagAdmin";
import openAbility from "./modules/openAbility";
import proAdmin from "./modules/proAdmin";
import dataAdmin from "./modules/dataAdmin";
import modelManage from "./modules/modelManage";
import user from "./modules/user";
import modelPublishAdmin from "./modules/modelPublishAdmin";
// import flow from './modules/flow'
import dictAdmin from "./modules/dictAdmin";
import itemAdmin from "./modules/itemAdmin";
import feCanvas from "./modules/feCanvas";
import consolePlat from "./modules/console";
import feOpr from "./modules/feOpr";
import codeDetect from "./modules/codeDetect";
import roleAdmin from "./modules/roleAdmin";
import featureEngineer from "./modules/featureEngineer";
import codeCut from "./modules/codeCut";
import dataSysAdmin from "./modules/dataSysAdmin";
import helpInfo from "./modules/helpInfo";
import menuAdmin from "./modules/menuAdmin";
import userAdmin from "./modules/userAdmin";
Vue.use(Vuex);

export default new Vuex.Store({
    // namespace: true,
    state: {
        currentMenu: null,
        //当前菜单
        tabsList: [
            {
                path: "/home",
                name: "Home",
                title: "首页"
            }
        ],
        userName: null,
        userId: null,
        token: null,
        breadcrumb: [], // { title: '页面名称', router: '页面路由' }
        navCollapse: false,
        user: {
            id: "",
            status: "", // -1 被删除，1.普通用户，2.管理员
            userName: ""
        },
        webName: "",
        /*
         * 权限管理copy
         *
         * */
        hasRoutes: false,
        editableTabsValue: "/index",
        editableTabs: [
            {
                title: "首页",
                name: "/index"
            }
        ]
    },
    mutations: {
        //选择标签 选择面包屑
        selectMenu(state, val) {
            // console.log(state,val)
            // if (val.name !== 'index'&&state.menuList.indexOf(val.name)==-1) {
            //     state.menuList = state.menuList.concat(val.name)
            //     state.tabsList.push({
            //       path: val.path,
            //       name: val.name,
            //       title:val.meta.title,
            //     })
            // } else{
            //   state.currentMenu  = null
            // }
        },
        //关闭标签
        closeTab(state, val) {
            let result = state.tabsList.findIndex(
                item => item.name === val.name
            );
            state.tabsList.splice(result, 1);
            state.splice(result.name, 1);
        },
        // SET_TOKEN: (state, token) => {
        //     state.token = token
        // },
        SET_WEB_NAME: (state, webName) => {
            state.webName = webName;
        },
        SET_USER_NAME: (state, userName) => {
            state.userName = userName;
        },
        SET_USER_ID: (state, userId) => {
            state.userId = userId;
        },
        SET_BREADCRUMB(state, breadcrumb = []) {
            state.breadcrumb = breadcrumb;
        },
        SET_NAV_COLLAPSE(state, status = false) {
            state.navCollapse = status;
        },
        SET_USER(state, user) {
            state.user = user;
        },

        /*
         * 权限管理copy
         *
         * */
        SET_TOKEN: (state, token) => {
            // state.token = token
            sessionStorage.setItem("token", token);
        },
        RESET_TOKEN: state => {
            console.log("xxxx");
            //state.token=''
            sessionStorage.setItem("token", "");
        },
        SET_MENULIST: (state, menuList) => {
            //state.menuList=menuList;
            sessionStorage.setItem("menuList", JSON.stringify(menuList));
        },
        SET_PERMS: (state, perms) => {
            sessionStorage.setItem("perms", JSON.stringify(perms));
        },
        SET_USERINFO: (state, userInfo) => {
            console.log("xxx=" + JSON.stringify(userInfo));
            sessionStorage.setItem("userInfo", JSON.stringify(userInfo));
        },
        ADD_TABS: (state, tab) => {
            if (state.editableTabs.findIndex(e => e.name === tab.path) === -1) {
                state.editableTabs.push({
                    title: tab.name,
                    name: tab.path
                });
            }
            state.editableTabsValue = tab.path;
        },
        RESET_TABS: state => {
            state.editableTabsValue = "/index";
            state.editableTabs = [
                {
                    title: "首页",
                    name: "/index"
                }
            ];
        },
        SET_ROUTES_STATE: (state, hasRoutes) => {
            state.hasRoutes = hasRoutes;
        }
    },
    actions: {
        /*
         * 权限管理copy
         *
         * */
        logout() {
            window.sessionStorage.clear();
            this.state.hasRoutes = false;
        }
    },
    modules: {
        tagAdmin,
        openAbility,
        proAdmin,
        dataAdmin,
        modelManage,
        user,
        modelPublishAdmin,
        // flow,
        dictAdmin,
        itemAdmin,
        feCanvas,
        consolePlat,
        feOpr,
        codeDetect,
        roleAdmin,
        featureEngineer,
        codeCut,
        dataSysAdmin,
        helpInfo,
        menuAdmin,
        userAdmin,
    },
    getters: {
        userId: state => state.userId,
        token: state => state.token,
        userName: state => state.userName,
        breadcrumb: state => state.breadcrumb,
        navCollapse: state => state.navCollapse,
        user: state => state.user,

        /*
         * 权限管理copy
         *
         * */
        GET_TOKEN: state => {
            return sessionStorage.getItem("token");
        },
        GET_MENULIST: state => {
            return JSON.parse(sessionStorage.getItem("menuList"));
        },
        GET_PERMS: state => {
            return JSON.parse(sessionStorage.getItem("perms"));
        },
        GET_USERINFO: state => {
            // console.log("GET_USERINFO=" + sessionStorage.getItem("userInfo"))
            return JSON.parse(sessionStorage.getItem("userInfo"));
        }
    }
});
